import 'package:flutter/material.dart';

/// Navigator负责管理维护打开页面的堆栈
/// 压一个新的页面到屏幕上:Navigator.of(context).push
/// 把路由顶层的页面移除:Navigator.of(context).pop
///
/// BuildContext对象实际上就是Element对象，BuildContext 接口用于阻止对 Element 对象的直接操作。
void main() => runApp(BuildContextApp());

class BuildContextApp extends StatelessWidget {
  // 该context对象，对应当前Widget对象创建的Element对象，记录当前构建的对象在该树的什么位置
  // 可以往上查找一些信息
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: PageOne(),
    );
  }
}

/// PageOne的跳转代码直接写到BuildContextApp类中点击跳转报错：
///     flutter: Navigator operation requested with a context that does not include a Navigator.
///     flutter: The context used to push or pop routes from the Navigator must be that of a widget that is a
///      flutter: descendant of a Navigator widget.
/// 如果将界面拆开就不会出错，因为of(context)会向上寻找Widget创建的Navigator

class PageOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          child: Text('跳转'),
          onPressed: () => Navigator.push(
              context, MaterialPageRoute(builder: (context) => PageTwo())),
        ),
      ),
    );
  }
}

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          child: Text('PageTwo'),
        ),
      ),
    );
  }
}
